﻿var info = document.getElementById('map-show-info');
var map = document.getElementById('map-info');
var gmap;
var gmarker;

if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError, {enableHighAccuracy:true,
              maximumAge:30000,
              timeout:27000});
			  /* 
			     enableHighAccuracy -> true 是否獲得高精準度設備結果(較耗電)   
			     maximumAge -> 30000 緩存位置的時間(毫秒數),在此時間內使用緩存位置不更新 若設為0=永遠使用最新位置
			     timeout -> 27000 預設無窮大 獲取位置時間超過此時間會觸發錯誤
			   */
        //alert( ' 瀏覽器支援 geolocation ' );
}else{
        alert( ' 瀏覽器不支援 geolocation ' );
}




function getPositionSuccess(position){
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
		var alt = position.coords.altitude;
		
		var heading = position.coords.heading;
		var speed = position.coords.altitude;

		/*
		//定義持續追蹤位置 測試中
		geolocation.watchPosition(refreshPosition, getPositionError, {enableHighAccuracy:true,
              maximumAge:30000,
              timeout:27000});
        */
 
		//載入 Google 地圖
		var latLng = new google.maps.LatLng(lat,lon);
		var mapOptions = {zoom:14,
		                  center:latLng,
						  mapTypeId:google.maps.MapTypeId.ROADMAP
		};
		gmap = new google.maps.Map(map,mapOptions);
		//添加標記
		gmarker = new google.maps.Marker({
		     position:latLng,
			 map:gmap
		});
		/* 
		等同於
		gmarker = new google.maps.Marker();
		gmarker.setPosition(latLng);
        gmarker.setMap(gmap);
		*/
		
		var infowindow_g = new google.maps.InfoWindow({ 
			content: 'You are here'
		});

		google.maps.event.addListener(gmarker, 'click', function() {
			infowindow_g.open(gmap,gmarker);
		});
		// new updated 'You are here'
		
		
		
		getCurrentUsers(gmap);
		
		
		
		info.innerHTML = "<p>您所在的位置： 經度" + lat + "，緯度" + lon + "，海拔" + alt + "，設備移動方向" + heading + "，設備移動速度" + speed + "</p>";
		/*
        alert( "您所在的位置： 經度" + lat + "，緯度" + lon + "，海拔" + alt + "，設備移動方向" + heading + "，設備移動速度" + speed );
        */
		if(typeof position.address !== "undefined"){
                var country = position.address.country;
                var province = position.address.region;
                var city = position.address.city;
				
				info.innerHTML += "<p> 您位於 " + country + " " + province + " " + city + "</p>";
				/*
                alert(" 您位於 " + country + " " + province + " " + city );
				*/
        }
}

function refreshPosition(position){
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;

        var latLng = new google.maps.LatLng(lat,lon);
		//重設地圖位置
		gmap.setCenter(latLng);
		//重設標記位置
		gmarker.setPosition(latLng);
}


function getPositionError(error){
    switch(error.code){
        case error.TIMEOUT :
            alert( " 連接超時，請重試 " );
            break;
        case error.PERMISSION_DENIED :
            alert( " 您拒絕透露您的位置，查詢已取消 " );
            break;
        case error.POSITION_UNAVAILABLE :
            alert( " 親愛的網友，非常抱歉，我們暫時無法為您提供服務 " );
            break;
    }
}